#include <stdio.h>
#include <string.h>
#include "DES.h"

#define MAX_LENGTH 10000
int main(int argc, char** argv)
{
  int i,j;
  char c;
  FILE *fp1,*fp2;
  if(argc != 4) {
    fputs("Usage: encrypt KEY INPUT_FILE OUTPUT_FILE\n", stderr);
    return 1;
  } else if ( strlen(argv[1]) != 8 ) {
    fputs("KEY must be exactly 8 bytes long\n",stderr);
    return 2;
  }
  /* Convert 64-bit DES key to 56-bit DES key by dropping parity bits*/
  uchar key[7];
  for(i=0;i<8;i++) {
    argv[1][i]<<=1;		
  }
  auth_DESkey8to7( key, argv[1] );
 
  /* Initialize input to null-filled buffer */
  uchar input[MAX_LENGTH];  
  for(i = 0; i < MAX_LENGTH; i++)
    input[i] = '\0';

  /* Initialize output to null-filled buffer */
  uchar output[MAX_LENGTH];
  for(i = 0; i < MAX_LENGTH; i++)
    output[i] = '\0';
	
	/* Open files for plain text & cipher text */
	fp1=fopen(argv[2],"rb");
	fp2=fopen(argv[3],"wb");

  /* Read plain text from INPUT_FILE */
  i=0;
  while((c=fgetc(fp1))!=EOF){
    input[i++]=c;    
  }

  /* Determine total number of blocks read */
  int blocks = (strlen(input)/8)+1;
  
  /* Encrypt each block one at a time */
  for(i = 0; i < blocks; i++) {
    auth_DEShash( output+i*8, key, input+i*8 );    
  }

  /* Print result to OUTPUT_FILE */
  for(i = 0; i < blocks*8; i++) {
    fprintf(fp2,"%c",output[i]);   
  }

  return 0;
}

